package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bangdream.michelia.R;
import com.bangdream.michelia.application.MainApplication;
import com.bangdream.michelia.entity.exam.AnswerInfoListBean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @date on 2018/8/18 23:25
 * @email 909732184@qq.com
 * 选择题的答案选择适配器
 */
public class SelectExamAdapter extends RecyclerView.Adapter {
    private List<AnswerInfoListBean> data = new ArrayList<>();
    private Context context;
    private LayoutInflater layoutInflater;
    private OnSelectExamItemClickListener onSelectExamItemClickListener;
    // 用来控制CheckBox的选中状况 isSelected中存在的则为选中状态
    private LinkedHashSet<Integer> isSelected = new LinkedHashSet<>();
    //单选还是多选
    private boolean isSingle = true;
    //最大选择数量
    private int MaxSelectNumb = 1;
    private int SelectColor,SelectedColor;
    public boolean isSingle() {
        return isSingle;
    }

    public void setSingle(boolean single) {
        isSingle = single;
    }

    public int getMaxSelectNumb() {
        return MaxSelectNumb;
    }

    public void setMaxSelectNumb(int maxSelectNumb) {
        MaxSelectNumb = maxSelectNumb;
    }

    public Set<Integer> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(LinkedHashSet<Integer> isSelected) {
        this.isSelected = isSelected;
    }

    public List<AnswerInfoListBean> getData() {
        return data;
    }

    public void setData(List<AnswerInfoListBean> data) {
        this.data = data;
    }

    public OnSelectExamItemClickListener getOnSelectExamItemClickListener() {
        return onSelectExamItemClickListener;
    }

    public void setOnSelectExamItemClickListener(OnSelectExamItemClickListener onSelectExamItemClickListener) {
        this.onSelectExamItemClickListener = onSelectExamItemClickListener;
    }

    public SelectExamAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        SelectColor = ContextCompat.getColor(context,R.color.common_red);
        SelectedColor = ContextCompat.getColor(context,R.color.title);
    }

    public interface OnSelectExamItemClickListener{
        void onSelectExamItemClick(int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //View view = layoutInflater.inflate(R.layout.select_exam_item,parent,false);
        return new ViewHolder(layoutInflater.inflate(R.layout.select_exam_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.position = position;
        // 根据isSelected来设置checkbox的选中状况
        String[]  str1 = new String[]{"A","B","C","D","E","F","G","H","I"};
        viewHolder.tvChoose.setText(str1[position]);
        if (isExistence(position)) {
            viewHolder.ivIcon.setImageResource(R.mipmap.kaoshi_ys);
            viewHolder.tvText.setTextColor(SelectColor);
            viewHolder.tvChoose.setTextColor(SelectColor);
        }else{
            viewHolder.ivIcon.setImageResource(R.mipmap.kaoshi_bj_answer_a);
            viewHolder.tvText.setTextColor(SelectedColor);
            viewHolder.tvChoose.setTextColor(SelectedColor);
        }
        viewHolder.tvText.setText(data.get(position).getDescription()+"");
    }

    private boolean isExistence(int position) {
        //return isSelected.containsKey(position);
        return isSelected.contains(position);
    }

    //点击选中或取消选中
    public void SetSelect(int position)
    {
        if (isExistence(position)) {
            isSelected.remove(position);
        } else {
            isSelected.add(position);
        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        public int position;
        private ImageView ivIcon;
        private TextView tvText;
        private TextView tvChoose;
        public ViewHolder(final View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.ivIcon);
            tvText = itemView.findViewById(R.id.tvText);
            tvChoose = itemView.findViewById(R.id.tv_choose);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClick(position);
                    if(onSelectExamItemClickListener!=null){
                        onSelectExamItemClickListener.onSelectExamItemClick(position);
                    }
                }
            });
        }
    }
    //点击item时的内部处理
    private void itemClick(int position){
        //单选 或者多选
        if(isSingle){
            for(int i :isSelected){
                notifyItemChanged(i);
            }
            isSelected.clear();
            SetSelect(position);
            notifyItemChanged(position);
        }else {
            if(!isExistence(position)){
                //不能大于最大可选择数量
                if(isSelected.size()<MaxSelectNumb){
                    SetSelect(position);
                }else {
                    Toast.makeText(MainApplication.getContext(),"最多选择"+MaxSelectNumb+"条",Toast.LENGTH_LONG).show();
                }
            }else {
                SetSelect(position);
            }
            notifyItemChanged(position);
        }
    }
}
